package com.itheima.reggie.controller;


import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.entity.dto.SetmealDishDto;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 套餐保存功能
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto){
        Long empId = (Long) session.getAttribute("Employee");
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize, String name){
        R<Page<SetmealDto>> result = setmealService.findByPage(page,pageSize,name);

        return result;

    }

    /**
     * 删除套餐功能
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 批量启售和停售功能
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐状态修改接口")
    public R<String> changeStatus(@PathVariable Integer status,
                                  @RequestParam List<Long> ids){
        setmealService.changeStatus(status,ids);
        return R.success("修改成功");
    }

    /**
     * 修改套餐功能
     * @param setmealDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "套餐修改接口")
    public R<String> update(@RequestBody SetmealDto setmealDto){
        Long empId = (Long) session.getAttribute("Employee");
        setmealDto.setUpdateUser(empId);
        setmealService.update(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 回显
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    @ApiOperation(value = "套餐修改窗口数据回显接口")
    public R<SetmealDto> findById(@PathVariable Long setmealId){
        R<SetmealDto> result = setmealService.findById(setmealId);
        return result;
    }

    /**
     * 移动端查看套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> list(Long categoryId, Integer status){
        List<Setmeal> setmealList = setmealService.list(categoryId,status);
        return R.success(setmealList);
    }

    /**
     * 移动端查看套餐详情
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<SetmealDishDto>> findBySetmeal(@PathVariable("id") Long id){
        R<List<SetmealDishDto>> result =  setmealService.findBySId(id);
        return result;
    }
}